package com.usiful.app;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.usiful.controller.AppPropertiesController;
import com.usiful.controller.BaseController;
import com.usiful.controller.BuildController;
import com.usiful.controller.GlobalSettingController;
import com.usiful.controller.GradleScriptFilterController;
import com.usiful.controller.PacketController;
import com.usiful.controller.PluginController;
import com.usiful.controller.SignController;
import com.usiful.model.ActionArg;
import com.usiful.utils.PathUtil;

/**
 * 变色龙构建工具
 * 
 * @author [周织能]
 *
 */
public class Usiful {

	protected static Map<String, BaseController> g_ControllerMap;
	protected static Map<String, ActionArg> g_ActionArgMap;
	protected static Map<String,BaseController> g_ActionArgControllerMap;
	
	public static void test() {
		File file = new File("/Users/zhouzhineng/Desktop/cordova/chameleon-app/chameleon-android/platforms/android/AndroidManifest.xml");
		System.out.println(file.getParent());
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//String[]test_args = {ActionArg.BUILD,"--path=/Users/zhouzhineng/Desktop/git_workplace/android-engine-ace-test","--build=bundle"};
		//String[] test_args = {ActionArg.PACKET,"--help"};
		//String[] test_args = {ActionArg.APP_PROPERTIES,"--path=/Users/zhouzhineng/Desktop/git_workplace/android-engine-ace-test","--package=com.hello.fuck.bsl","--vercode=1","--vername=1.0.1","--appname=变态龙","--appicon=/Users/zhouzhineng/Downloads/105121_8Krb_28.png","--splash=/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_phone_l_960x640.png,/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_pad_p_640x1136.png,/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_phone_p_640x960.png,/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_pad_l_1136x640.png"};
		//String[] test_args = {ActionArg.PACKET,"--path=/Users/zhouzhineng/Desktop/cordova/chameleon-app/chameleon-android/platforms/android","--keystore=/Users/zhouzhineng/Desktop/find-master-android-822/product.keystore","--alias=foss","--keypass=foreveross2016","--aliaspass=foreveross2016","--apkdir=/Users/zhouzhineng/Desktop/out_apk","--sdkpath=/Users/zhouzhineng/Downloads/adt-bundle-mac-x86_64-20140702/sdk/"};
		//String[] test_args = {ActionArg.PLUGIN,"--project=/Users/zhouzhineng/Desktop/git_workplace/android-engine-ace-test","--operateType=add","--file=/Users/zhouzhineng/Desktop/git_workplace/pingpp","--apppackage=com.foreveross.bsl.test"};
		//String[] test_args = {ActionArg.PLUGIN,"--project=/Users/zhouzhineng/Desktop/git_workplace/android-engine-ace-test","--operateType=remove","--package=com.foreveross.chameleon.user.plugin","--name=PingPlugin"};
		//String[] test_args = {ActionArg.GLOBAL_SETTING,"--path=/Users/zhouzhineng/Desktop/git_workplace/android-engine-ace-test","--operateType=set","--key=sdk.dir","--value=/Users/zhouzhineng/Downloads/macosx-android-sdk"};
		//args = test_args;
		System.out.println("args=======>" + args.toString());
		g_ControllerMap = new HashMap<String, BaseController>();
		g_ControllerMap.put(AppPropertiesController.class.getName(), new AppPropertiesController());
		g_ControllerMap.put(PluginController.class.getName(), new PluginController());
		g_ControllerMap.put(BuildController.class.getName(), new BuildController());
		g_ControllerMap.put(SignController.class.getName(), new SignController());
		g_ControllerMap.put(GradleScriptFilterController.class.getName(), new GradleScriptFilterController());
		g_ControllerMap.put(GlobalSettingController.class.getName(), new GlobalSettingController());
		g_ControllerMap.put(PacketController.class.getName(),
				new PacketController(
								(AppPropertiesController) g_ControllerMap.get(AppPropertiesController.class.getName()),
								(BuildController) g_ControllerMap.get(BuildController.class.getName()),
								(SignController) g_ControllerMap.get(SignController.class.getName())
				)
		);
		g_ActionArgMap = new HashMap<String, ActionArg>();
		g_ActionArgMap.put(AppPropertiesController.class.getName(), new ActionArg(ActionArg.APP_PROPERTIES));
		g_ActionArgMap.put(PluginController.class.getName(), new ActionArg(ActionArg.PLUGIN));
		g_ActionArgMap.put(BuildController.class.getName(), new ActionArg(ActionArg.BUILD));
		g_ActionArgMap.put(SignController.class.getName(), new ActionArg(ActionArg.SIGN));
		g_ActionArgMap.put(PacketController.class.getName(), new ActionArg(ActionArg.PACKET));
		g_ActionArgMap.put(GradleScriptFilterController.class.getName(),
				new ActionArg(ActionArg.GRADLE_SCRIPT_FILTER));
		g_ActionArgMap.put(GlobalSettingController.class.getName(),new ActionArg(ActionArg.GLOBAL_SETTING));
		
		g_ActionArgControllerMap = new HashMap<String,BaseController>();
		g_ActionArgControllerMap.put(g_ActionArgMap.get(AppPropertiesController.class.getName()).getActionName(), g_ControllerMap.get(AppPropertiesController.class.getName()));
		g_ActionArgControllerMap.put(g_ActionArgMap.get(PluginController.class.getName()).getActionName(), g_ControllerMap.get(PluginController.class.getName()));
		g_ActionArgControllerMap.put(g_ActionArgMap.get(BuildController.class.getName()).getActionName(), g_ControllerMap.get(BuildController.class.getName()));
		g_ActionArgControllerMap.put(g_ActionArgMap.get(SignController.class.getName()).getActionName(), g_ControllerMap.get(SignController.class.getName()));
		g_ActionArgControllerMap.put(g_ActionArgMap.get(PacketController.class.getName()).getActionName(), g_ControllerMap.get(PacketController.class.getName()));
		g_ActionArgControllerMap.put(g_ActionArgMap.get(GradleScriptFilterController.class.getName()).getActionName(), g_ControllerMap.get(GradleScriptFilterController.class.getName()));
		g_ActionArgControllerMap.put(g_ActionArgMap.get(GlobalSettingController.class.getName()).getActionName(), g_ControllerMap.get(GlobalSettingController.class.getName()));
		
		
		initActionArgParamMap();
		try {
			handleArgActions(args);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	
	private static void initActionArgParamMap() {
		Map<String,String> paramDescSet = null; //param,required?
		paramDescSet = g_ActionArgMap.get(AppPropertiesController.class.getName()).getArgParamDescSet();
		{			
			paramDescSet.put(AppPropertiesController.ARG_PROJECT_PATH,"项目路径[require]");
			paramDescSet.put(AppPropertiesController.ARG_PKGNAME,"app包名[option]");
			paramDescSet.put(AppPropertiesController.ARG_VERCODE,"app版本号[option]");
			paramDescSet.put(AppPropertiesController.ARG_VERNAME,"app版本字符[option]");
			paramDescSet.put(AppPropertiesController.ARG_MINSDK,"app最低支持的sdk版本[option]");
			paramDescSet.put(AppPropertiesController.ARG_TARGETSDK,"app当前使用的sdk版本[option]");
			paramDescSet.put(AppPropertiesController.ARG_APPNAME,"app名称[option]");
			paramDescSet.put(AppPropertiesController.ARG_APPICON,"app图标[option]");
			paramDescSet.put(AppPropertiesController.ARG_SPLASH,"app启动的欢迎界面[option]");
			paramDescSet.put(AppPropertiesController.ARG_ORIENTATION,"app横竖屏[option]");
			paramDescSet.put(AppPropertiesController.ARG_CONFIGLINE,"app自定义参数[option]");
			
		}
		paramDescSet = g_ActionArgMap.get(PluginController.class.getName()).getArgParamDescSet();
		{
			paramDescSet.put(PluginController.ARG_PROJECT_PATH,"项目路径[required]");
			paramDescSet.put(PluginController.ARG_OPERATE_TYPE,"操作类型(add/remove)[required]");
			paramDescSet.put(PluginController.ARG_PLUGIN_NAME,"插件名[required for remove]");
			paramDescSet.put(PluginController.ARG_PLUGIN_PATH,"插件路径[required for add]");
			paramDescSet.put(PluginController.ARG_PLUGIN_PKGNAME,"插件包名[required for remove]");
			paramDescSet.put(PluginController.ARG_APP_PKGNAME,"app包名[required for add]");
		}
		paramDescSet = g_ActionArgMap.get(BuildController.class.getName()).getArgParamDescSet();
		{
			paramDescSet.put(BuildController.ARG_PROJECT_PATH,"项目路径[require]");
		}
		paramDescSet = g_ActionArgMap.get(SignController.class.getName()).getArgParamDescSet();
		{
			paramDescSet.put(SignController.ARG_PROJECT_PATH,"项目路径[required]");
			paramDescSet.put(SignController.ARG_KEYSTORE,"app签名文件路径[required]");
			paramDescSet.put(SignController.ARG_ALIAS,"app签名文件的alias[required]");
			paramDescSet.put(SignController.ARG_KEYPASS,"app签名文件keypass[required]");
			paramDescSet.put(SignController.ARG_STOREPASS,"app签名文件storepass[required]");
			paramDescSet.put(SignController.ARG_SIGN_APKDIR,"签名后的app输出路径[required]");
		}
		
		paramDescSet = g_ActionArgMap.get(PacketController.class.getName()).getArgParamDescSet();
		{
			paramDescSet.put(PacketController.ARG_SDK_PATH,"sdk路径[required]");;
			paramDescSet.put(PacketController.ARG_PROJECT_PATH,"项目路径[required]");
			paramDescSet.put(PacketController.ARG_PKGNAME,"app包名[option]");
			paramDescSet.put(PacketController.ARG_VERCODE,"app版本号[option]");
			paramDescSet.put(PacketController.ARG_VERNAME,"app版本字符[option]");
			paramDescSet.put(PacketController.ARG_MINSDK,"app最低支持的sdk版本[option]");
			paramDescSet.put(PacketController.ARG_TARGETSDK,"app当前使用的sdk版本[option]");
			paramDescSet.put(PacketController.ARG_APPNAME,"app名称[option]");
			paramDescSet.put(PacketController.ARG_APPICON,"app图标[option]");
			paramDescSet.put(PacketController.ARG_SPLASH,"app启动的欢迎界面[option]");
			paramDescSet.put(PacketController.ARG_ORIENTATION,"app横竖屏[option]");
			paramDescSet.put(PacketController.ARG_CONFIGLINE,"app自定义参数[option]");
			paramDescSet.put(PacketController.ARG_KEYSTORE,"app签名文件路径[required]");
			paramDescSet.put(PacketController.ARG_ALIAS,"app签名文件的alias[required]");
			paramDescSet.put(PacketController.ARG_KEYPASS,"app签名文件的keypass[required]");
			paramDescSet.put(PacketController.ARG_STOREPASS,"app签名文件storepass[required]");
			paramDescSet.put(PacketController.ARG_SIGN_APKDIR,"签名后的app输出路径[required]");
			
		}
		paramDescSet = g_ActionArgMap.get(GradleScriptFilterController.class.getName()).getArgParamDescSet();
		{
			paramDescSet.put(GradleScriptFilterController.ARG_PROJECT_PATH,"项目路径[require]");
		}
		
		paramDescSet = g_ActionArgMap.get(GlobalSettingController.class.getName()).getArgParamDescSet();
		{
			paramDescSet.put(GlobalSettingController.ARG_OPERATE_TYPE,"操作类型(set/get)[required]");
			paramDescSet.put(GlobalSettingController.ARG_KEY,"键名[required]");
			paramDescSet.put(GlobalSettingController.ARG_VALUE,"键值[option]");
			
		}
	}
	/*
	 * (non-Java-doc)
	 * 
	 * @see java.lang.Object#Object()
	 */
	public Usiful() {
		super();
	}

	/**
	 * ./packet --path=
	 * "/Users/BSL/develop/build/pkg/ANDROID/0e78a6b2-e228-47e8-83fc-06b7f6202742/chameleon-qdt-android-app/"
	 * --apkdir="/Users/BSL/develop/build/pkg_app/ANDROID/2015-12-15/0/7/"
	 * --keystore=
	 * "/Users/BSL/develop/build/pkg/ANDROID/0e78a6b2-e228-47e8-83fc-06b7f6202742/chameleon-qdt-android-app/android.keystore"
	 * --alias="test_1" --keypass="test123" --aliaspass="test123"
	 * --buildtype="bundle" --logpath=
	 * "/Users/BSL/develop/build/pkg_log/ANDROID/2015-12-15/0/7/app.log"
	 * --appname="pushdemo" --appicon=
	 * "/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/1450163581076.png"
	 * --splash=
	 * "/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_phone_l_960x640.png,/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_pad_p_640x1136.png,/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_phone_p_640x960.png,/Users/BSL/develop/build/tmp/566fbd6644f4998ed89b6a5e/splash_pad_l_1136x640.png"
	 * --orientation="unspecified" --vercode="1" --vername="1.0.0"
	 * --package="com.pushdemo.app"
	 */
	
	/**
	 * 处理输入参数
	 * 
	 * @param args
	 * @throws Exception 
	 */
	private static void handleArgActions(String[] args) throws Exception {
		String action = args[0];	//第一个参数为action
		ActionArg actionArg = null;
		switch(action) {
		case ActionArg.APP_PROPERTIES:
			actionArg = g_ActionArgMap.get(AppPropertiesController.class.getName());
			break;
		case ActionArg.BUILD:
			actionArg = g_ActionArgMap.get(BuildController.class.getName());
			break;
		case ActionArg.SIGN:
			actionArg = g_ActionArgMap.get(SignController.class.getName());
			break;
		case ActionArg.PLUGIN:
			actionArg = g_ActionArgMap.get(PluginController.class.getName());
			break;
		case ActionArg.PACKET:
			actionArg = g_ActionArgMap.get(PacketController.class.getName());
			break;
		case ActionArg.GRADLE_SCRIPT_FILTER:
			actionArg = g_ActionArgMap.get(GradleScriptFilterController.class.getName());
			break;
		case ActionArg.GLOBAL_SETTING:
			actionArg = g_ActionArgMap.get(GlobalSettingController.class.getName());
			break;
		}
		if(actionArg == null) {
			 throw new Exception("Invaild Action Type!");
		}
		String argName,argInfoArr[],argValueArr[],delimite = "--";
		for(int i = 1; i < args.length; i++) {
			argInfoArr = args[i].split("=");
			List<String> argValues = new ArrayList<String>();
			if(argInfoArr.length != 2) {
				if(argInfoArr[0].replace(delimite, "").equals(ActionArg.HELP)) {
					actionArg.getArgValueMap().put(ActionArg.HELP, argValues);
					break;
				} else {
					continue;
				}
			} else {
				argName = argInfoArr[0].replace(delimite, "");
				argValueArr = argInfoArr[1].split(",");
				for(String argValue : argValueArr) {
					argValues.add(argValue);
				}
			}
			actionArg.getArgValueMap().put(argName, argValues);
		}
		
		g_ActionArgControllerMap.get(actionArg.getActionName()).handleAction(actionArg);
	}

	
}